<%--
  Created by IntelliJ IDEA.
  User: q
  Date: 2021/11/12
  Time: 17:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>03-RequestBody.jsp</title>
    <script src="${pageContext.request.contextPath}/js/jquery-3.4.1.min.js"></script> <%--得这样写--%>
    <script>
        $(function () {
            //alert("111");
            $("#btn").click(function () {
                var user = {
                    username : '李四',
                    gender : 0,
                    birth : '2002-5-20',
                    password : '123456'
                };

                $.ajax({
                    url : "test51",
                    type : "POST",
                    contentType : "application/json", //指定发送的数据格式为json格式
                    data : JSON.stringify(user),  //使用转换得到一个标准json格式
                    dataType : "json",  //预计服务器响应的数据类型
                    success : function (result) {
                        console.log(result);
                        console.log(result.username);
                    },
                    error : function () {
                        alert("error");
                    }
                })
            });
        })
    </script>
</head>
<body>
    <button id="btn">Ajax发送json数据</button>
</body>
</html>